/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.view;

import Utils.MyOwnFocusTraversalPolicy;
import Utils.TextFieldFilter;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.util.Vector;
import javax.swing.BorderFactory;

/**
 *
 * @author borysfan
 */
public class FuelSubPanel extends ContentPanel {

    private MyLabel takeOffFuelLab;
    private MyLabel tripFuelLab;
    private MyLabel taxiFuelLab;
    private MyLabel totalFuelLab;
    private MyLabel totalFuelValueLab;
    private MyLabel maxFuelLab;
    private MyLabel maxFuelValueLab;
    private MyLabel takeOffFuelControlLab;
    private MyLabel tripFuelControlLab;
    private MyLabel taxiFuelControlLab;
    private MyTextField takeOffFuelText;
    private MyTextField tripFuelText;
    private MyTextField taxiFuelText;
    private MyOwnFocusTraversalPolicy travelsalPolicy;

    public FuelSubPanel() {
        super();
        initPanel();
    }

    private void initPanel() {
        takeOffFuelLab = new MyLabel("TAKE-OFF FUEL", MyLabel.BOLD);
        tripFuelLab = new MyLabel("TRIP FUEL", MyLabel.BOLD);
        taxiFuelLab = new MyLabel("TAXI FUEL", MyLabel.BOLD);
        totalFuelLab = new MyLabel("TOTAL FUEL", MyLabel.BOLD);
        totalFuelValueLab = new MyLabel("0", MyLabel.BOLD);

        maxFuelLab = new MyLabel("MAX FUEL", MyLabel.BOLD);
        maxFuelValueLab = new MyLabel("4000", MyLabel.BOLD);
        takeOffFuelText = new MyTextField(10);
        takeOffFuelText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 5, false));
        tripFuelText = new MyTextField(10);
        tripFuelText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 5, false));
        taxiFuelText = new MyTextField(10);
        taxiFuelText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 3, false));

        takeOffFuelControlLab = new MyLabel("           ");
        takeOffFuelControlLab.setVisible(false);
        tripFuelControlLab = new MyLabel("             ");
        tripFuelControlLab.setVisible(false);
        taxiFuelControlLab = new MyLabel("             ");
        taxiFuelControlLab.setVisible(false);

        setLayout(null);

        add(takeOffFuelLab);
        add(tripFuelLab);
        add(taxiFuelLab);
        add(totalFuelLab);
        add(totalFuelValueLab);
        add(maxFuelLab);
        add(maxFuelValueLab);
        add(takeOffFuelText);
        add(tripFuelText);
        add(taxiFuelText);
        add(takeOffFuelControlLab);
        add(tripFuelControlLab);
        add(taxiFuelControlLab);

        takeOffFuelLab.setPositionByRight(new Point(250, 40));
        tripFuelLab.setPositionByRight(new Point(250, 70));
        taxiFuelLab.setPositionByRight(new Point(250, 100));
        totalFuelLab.setPositionByRight(new Point(250, 170));
        totalFuelValueLab.setPosition(new Point(275, 170));
        totalFuelValueLab.setSize(100, totalFuelValueLab.getSize().height);
        maxFuelLab.setPositionByRight(new Point(250, 200));
        maxFuelValueLab.setPreferredSize(new Dimension(maxFuelValueLab.getSize().width + 50, maxFuelValueLab.getPreferredSize().height));
        maxFuelValueLab.setPosition(new Point(300, 200));
        takeOffFuelText.setPosition(new Point(255, 40));
        tripFuelText.setPosition(new Point(255, 70));
        taxiFuelText.setPosition(new Point(255, 100));
        takeOffFuelControlLab.setPosition(new Point(260 + takeOffFuelText.getWidth(), 40));
        takeOffFuelControlLab.setForeground(Color.red);
        tripFuelControlLab.setPosition(new Point(260 + tripFuelText.getWidth(), 70));
        tripFuelControlLab.setForeground(Color.red);
        taxiFuelControlLab.setPosition(new Point(260 + taxiFuelText.getWidth(), 100));
        taxiFuelControlLab.setForeground(Color.red);
        serOrderList();
    }

    public void serOrderList() {
        Vector<Component> focusOrder = new Vector<Component>();
        focusOrder.add(takeOffFuelText);
        focusOrder.add(tripFuelText);
        focusOrder.add(taxiFuelText);
        travelsalPolicy = new MyOwnFocusTraversalPolicy(focusOrder);
        this.setFocusTraversalPolicy(travelsalPolicy);
        this.setFocusTraversalPolicyProvider(true);
        this.setFocusCycleRoot(true);
    }

    public void setTakeOffText(String s) {
        takeOffFuelText.setText(s);
    }

    public String getTakeOffText() {
        return takeOffFuelText.getText();
    }

    public void setTripFuelText(String s) {
        tripFuelText.setText(s);
    }

    public String getTripFuelText() {
        return tripFuelText.getText();
    }

    public void setTaxiFuelText(String s) {
        taxiFuelText.setText(s);
    }

    public String getTaxiFuelText() {
        return taxiFuelText.getText();
    }

    public void setTotalFuelLabValue(String value) {
        totalFuelValueLab.setText(value);
    }

    public String getTotalFuelLabValue() {
        return totalFuelValueLab.getText();
    }

    public void setMaxFuelLabValue(String value) {
        maxFuelValueLab.setText(value);
    }

    public String getMaxFuelLabValue() {
        return maxFuelValueLab.getText();
    }

    public void setTakeOffFocusListener(FocusListener listener) {
        takeOffFuelText.addFocusListener(listener);
    }

    public void setTaxiFuelFocusListener(FocusListener listener) {
        taxiFuelText.addFocusListener(listener);
    }

    public void setTripFuelFocusListener(FocusListener listener) {
        tripFuelText.addFocusListener(listener);
    }

    void reset() {
        takeOffFuelText.setText("0");
        tripFuelText.setText("0");
        taxiFuelText.setText("0");
        maxFuelValueLab.setText("0");
        totalFuelValueLab.setText("0");
    }

    /*gettery do obslugi preview*/
    public String get_vtof() {
        return takeOffFuelText.getText();
    }

    String get_vtf() {
        return tripFuelText.getText();
    }

    String get_vtxf() {
        return taxiFuelText.getText();
    }

    void setVtofControlerText(String s) {
        takeOffFuelControlLab.setText(s);
        takeOffFuelControlLab.setSize(170, takeOffFuelControlLab.getHeight());
    }

    void setVtofControlerTextVisible(boolean b) {
        takeOffFuelControlLab.setVisible(b);
    }

    void setTaxiFuelControlLab(String s) {
        taxiFuelControlLab.setText(s);
        taxiFuelControlLab.setSize(170, taxiFuelControlLab.getHeight());
    }

    void setTaxiFuelControlLabVisible(boolean b) {
        taxiFuelControlLab.setVisible(b);
    }

    void setTripFuelControlText(String s) {
        tripFuelControlLab.setText(s);
        tripFuelControlLab.setSize(170, tripFuelControlLab.getHeight());
    }

    void setTripFuelControlVisible(boolean b) {
        tripFuelControlLab.setVisible(b);
    }

    void setTofBorder(Color c) {
        takeOffFuelText.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    void setTfBorder(Color c) {
        tripFuelText.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    void setTaxiFuelBorder(Color c) {
        taxiFuelText.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    public void firstFocused() {
        takeOffFuelText.requestFocus();
    }

    public void removeActionsListeners() {
        ActionListener[] listeners = takeOffFuelText.getActionListeners();
        for (int i = 0; i < listeners.length; i++) {
            takeOffFuelText.removeActionListener(listeners[i]);
        }

        listeners = tripFuelText.getActionListeners();
        for (int i = 0; i < listeners.length; i++) {
            tripFuelText.removeActionListener(listeners[i]);
        }

        listeners = taxiFuelText.getActionListeners();
        for (int i = 0; i < listeners.length; i++) {
            taxiFuelText.removeActionListener(listeners[i]);
        }
    }

    @Override
    public boolean isValid() {
        try {

            if (takeOffFuelControlLab.isVisible()){
                return false;
            }
            if (tripFuelControlLab.isVisible()){
                return false;
            }
            if (taxiFuelControlLab.isVisible()){
                return false;
            }
        } catch (NullPointerException e) {
            return false;
        }

        return true;
    }
}
